﻿ 
var posturl = "/EasyUI/Content/Add";
if ($("#addoredit").val() === "1")
    posturl = "/EasyUI/Content/Edit";

var ue = UE.getEditor("details",
    {
        autoHeight: false
    });

$("#categoryselect").combobox({
    editable: false,
    onChange: function (newValue, oldValue) {
        $("#categoryid").val(newValue);
    },
    url: '/EasyUI/Content/GetCategoryOption',
    valueField: 'id',
    textField: 'text'
});


$("#columnselect").combobox({
    editable: false,
    onChange: function (newValue, oldValue) {
        $("#columnid").val(newValue);
    },
    url: '/EasyUI/Content/GetCategoryOption',
    valueField: 'id',
    textField: 'text'
}); 

var categoryselect = $("#categoryselect").combobox("panel");
categoryselect.panel({ cls: "categoryselect-panel" });

var columnselect = $("#columnselect").combobox("panel");
columnselect.panel({ cls: "columnselect-panel" });

$("#catetory").form({
    url: posturl,
    onSubmit: function () {
        var res = $(this).form("enableValidation").form("validate");
        if (res) {

            if ($("#images").attr("src") === "/Areas/EasyUI/Scripts/Admin/uploadimage.jpg") {
                $.messager.alert("温馨提示", "请上传封面图片！", "error");
                return false;
            }

            if ($("#categoryid").val() === "") {
                $.messager.alert("温馨提示", "请选择文章类型！", "error");
                return false;
            }

            if ($("#columnid").val() === "") {
                $.messager.alert("温馨提示", "请选择指定栏目！", "error");
                return false;
            }

            if (!UE.getEditor("details").hasContents()) {
                $.messager.alert("温馨提示", "请填写内容！", "error");
                return false;
            }

            $.messager.progress();
        }
        return res;
    },
    success: function (result) {
        debugger;
        if (typeof (result) !== "object")
            result = JSON.parse(result);
        $.messager.progress("close");
        if (result !== null && typeof (result) === "object") {
            if (result.code === 200) {
                OperateMsg("授权提示", result.msg);
            } else if (result.code === 401)
                $.messager.alert("授权提示", result.msg, "error");
            else if (result.code === 402) {
                ChangeImage();
                $.messager.alert("异常提醒", result.msg, "error");
            } else
                $.messager.alert("异常提醒", "系统异常", "error");
        } else
            $.messager.alert("异常提醒", "系统异常", "error");
    },
    error: function () {
        $.messager.alert("异常错误", "异常错误", "error");
    }
});

$("#images").click(function () {
    $("#imagesubmit").click(); //模拟点击
    $("#imagesubmit").change(function () {
        $("#filesubmit").submit(); //模拟提交 
    });
});

$("#filesubmit").submit(function (event) {
    event.preventDefault();
    var formData = new FormData(this);
    $.ajax({
        type: "post",
        url: "/EasyUI/Content/Images",
        data: formData,
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false
    }).success(function (result) {
        debugger;
        if (typeof (result) !== "object")
            result = JSON.parse(result);
        if (result.code === 200) {
            $("#hiddenimages").val("/TitleImage/" + new Date().format("yyyy-MM-dd") + "/" + result.msg);
            $("#images").attr("src",
                GetRootUrl() + "/TitleImage/" + new Date().format("yyyy-MM-dd") + "/" + result.msg);
        } else if (result.code === 401 || result.code === 402 || result.code === 403 || result.code === 404)
            $.messager.alert("导入提示", result.msg, "error");
        else
            $.messager.alert("异常提醒", "系统异常", "error");
    }).fail(function (jqXhr, textStatus, errorThrown) {
        $.messager.alert("异常错误", "异常错误", "error");
    });
});